package longrise.phone.com.bjjt_henan.compensate;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;

/**
 * 进入快处快赔的安全提示，Created by Administrator on 2016/5/12.
 */
public class OpenGpsView extends LFView implements View.OnClickListener, MainActivity.OnActivityReturnBitmap {
    private Context context = null;
    private View view = null;
    private Button bt_opengps;
    private LinearLayout hn_btn_back;
    private TextView tv_title;

    public OpenGpsView(Context c) {
        super(c);
        this.context = c;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        if (context instanceof MainActivity) {
            ((MainActivity) context).setOnActivityReturnBitmap(this);
        }
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_opengps, null);
        bt_opengps = (Button) view.findViewById(R.id.bt_opengps);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");
    }

    private void regEvent(boolean b) {
        if (null != bt_opengps) {
            bt_opengps.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_opengps:
                // 转到手机设置界面，用户设置GPS
                Intent intent = new Intent(
                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                if (context instanceof MainActivity) {
                    ((MainActivity) context).startActivityForResult(intent, 0001); // 设置完成后返回到原来的界面
                }
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;

        }
    }

    @Override
    public void activityReturnBitmap(int requestCode, int resultCode, Intent data) {
        //if(resultCode == MainActivity.RESULT_OK && requestCode==0 ){
        if (requestCode == 0001) {
            closeForm();//跳转到安全提示界面
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            if (!locationManager
                    .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
                //跳转到提示打开gps界面
                OpenGpsView opengpsview = new OpenGpsView(context);
                FrameworkManager.getInstance().showNewForm(context, opengpsview);

            } else {
                //跳转到安全提示界面
                SafeWarningView safewarningview = new SafeWarningView(context);
                FrameworkManager.getInstance().showNewForm(context, safewarningview);
            }
        }
    }
}
